package kr.co.casually;

import kr.co.casually.preference.ConcertPreferences;
import kr.co.casually.util.Utils;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.Toast;

import com.facebook.android.DialogError;
import com.facebook.android.Facebook;
import com.facebook.android.Facebook.DialogListener;
import com.facebook.android.FacebookError;

public class SNSSettingActivity extends Activity implements OnClickListener {
	private SharedPreferences mPrefs;
	private Facebook mFacebook = new Facebook(Utils.FACEBOOK_APP_ID);

	ImageView btnFacebook;


	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.sns_act);
		initActitivty();
	}

	private void initActitivty() {
		mPrefs = PreferenceManager.getDefaultSharedPreferences(this);
		btnFacebook = (ImageView) findViewById(R.id.btn_sns_facebook);
		setFacebookBtnText();
		btnFacebook.setOnClickListener(this);

	}

	private void setFacebookBtnText() {
		if (getFacebookAccessToken()) {
			btnFacebook.setBackgroundResource(R.drawable.keyword_btn1_prs);
		} else {
			btnFacebook.setBackgroundResource(R.drawable.keyword_btn1);
		}
	}

	private boolean getFacebookAccessToken() {
		return (mPrefs
				.getString(ConcertPreferences.FACEBOOK_ACCESS_TOKEN, null) != null) ? true
				: false;
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		
		
		if (resultCode == RESULT_OK) {

			switch (requestCode) {
			case Utils.REQ_CODE_FACEBOOK:
				mFacebook.authorizeCallback(requestCode, resultCode, data);
				break;

			case Utils.REQ_CODE_TWITTER:

				break;

			case Utils.REQ_CODE_ME2DAY:

				break;

			default:
				break;
			}
		}

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_sns_facebook:
			if (getFacebookAccessToken()) {
				logout();
			} else {
				login();
			}
			break;

		default:
			break;
		}
	}


	private void login() {

		mFacebook.authorize2(this,
				new String[] { "publish_stream, user_photos, email" },
				new AuthorizeListener());
	}


	private void logout() {
		try {
			mFacebook.logout(this);
			Toast.makeText(this, getString(R.string.facebook_logout), 1000)
					.show();
			
			btnFacebook.setBackgroundResource(R.drawable.keyword_btn1);
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			ConcertPreferences.storeFacebookAccessTooken(
					this, null);
		}
	}

	// Facebook 인증후 처리를 위한 callback class
	public class AuthorizeListener implements DialogListener {
		@Override
		public void onCancel() {
			alert(getString(R.string.facebook_login_cancel));
		}

		@Override
		public void onComplete(Bundle values) {
			if (Utils.D) {
				ConcertPreferences.storeFacebookAccessTooken(
						SNSSettingActivity.this, mFacebook.getAccessToken());

				alert(getString(R.string.facebook_login_complete));
				btnFacebook.setBackgroundResource(R.drawable.keyword_btn1_prs);
			}
		}

		@Override
		public void onError(DialogError e) {
			alert(getString(R.string.facebook_login_error));
		}

		@Override
		public void onFacebookError(FacebookError e) {
			alert(getString(R.string.facebook_login_error));
		}
	}

	private void alert(String msg) {
		Toast.makeText(this, msg, 1000).show();
	}
}
